package edu.com.wjt219971332.controller;

import edu.com.wjt219971332.entity.Competition;
import edu.com.wjt219971332.entity.Result;
import edu.com.wjt219971332.entity.Singer;
import edu.com.wjt219971332.service.CompetitionService;
import edu.com.wjt219971332.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Controller
@RequestMapping("competition")
public class competitionController {
    @Autowired
    private CompetitionService competitionService;
    @Autowired
    private SingerService singerService;
    @RequestMapping("show")
    public String show(){
        return "competition/querycompetition";
    }

    @GetMapping
    @ResponseBody
    public Result queryByPage(Competition competition, @RequestParam("page") Integer page, @RequestParam("size")Integer size, @RequestParam(value = "sort", required = false)String sort) {
        PageRequest pageable = PageRequest.of(page-1, size);
        Page<Competition> result = this.competitionService.queryByPage(competition, pageable);
        return Result.ok("获取成功！",  result.getTotalElements(), result.get().collect(Collectors.toList()));
    }
    @RequestMapping("/select")
    @ResponseBody
    public Result select(@RequestParam("asinger") String asinger){
        return  Result.ok("操作成功！", this.competitionService.queryByusername(asinger));
    }

    @RequestMapping("/insertInit")
    public String registerInit(){
        return "competition/insert";
    }

    @RequestMapping("/insert")
    public String register(@ModelAttribute("competition") Competition competition){
        competitionService.insert(competition);
        return "redirect: ../login/loginInit";
    }

    @RequestMapping("/updateInit/{competitionid}")
    public String updateInit(@PathVariable("competitionid") Integer competitionid, Model model){
        Competition competition = competitionService.queryById(competitionid);
        model.addAttribute("competition", competition);
        return  "competition/update";
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@ModelAttribute("competition") Competition competition){
        return Result.ok("修改成功",this.competitionService.update(competition));
    }
    @RequestMapping("/deleteInit/{competitionid}")
    @ResponseBody
    public String deleteInit(@PathVariable("competitionid") Integer competitionid){
        competitionService.deleteById(competitionid);
        return "redirect:/login/loginshow";
    }
    @RequestMapping("/findInit/{competitionid}")
    @ResponseBody
    public ResponseEntity<Map<String, String>> findInit(@PathVariable("competitionid") Integer competitionid, Model model){
        Map<String, String> response = new HashMap<>();
        Competition competition = competitionService.queryById(competitionid);
        model.addAttribute("competition", competition);
        URI uri = UriComponentsBuilder.fromPath("/competition/findInit2/{competitionId}")
                .buildAndExpand(competitionid)
                .toUri();
        response.put("redirect", uri.toString());
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/findInit2/{competitionid}")
    public String findInit2(@PathVariable("competitionid") Integer competitionid, Model model){
        Competition competition = competitionService.queryById(competitionid);
        Singer asinger = singerService.queryBysingername(competition.getAsinger());
        Singer bsinger = singerService.queryBysingername(competition.getBsinger());
        model.addAttribute("asinger", asinger);
        model.addAttribute("bsinger", bsinger);
        model.addAttribute("competition", competition);
        Date currentTime = new Date();

        // 计算倒计时时间差
        long timeDifference = competition.getEndtime().getTime() - currentTime.getTime();

        // 将时间差传递给前端页面
        model.addAttribute("days", TimeUnit.MILLISECONDS.toDays(timeDifference));
        model.addAttribute("hours", TimeUnit.MILLISECONDS.toHours(timeDifference) % 24);
        model.addAttribute("minutes", TimeUnit.MILLISECONDS.toMinutes(timeDifference) % 60);
        model.addAttribute("seconds", TimeUnit.MILLISECONDS.toSeconds(timeDifference) % 60);
        return  "competition/find";
    }
}
